package com.dy.lock.screen.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dy.lock.screen.constants.Constant;
import com.dy.lock.screen.dao.CustomerAuthenticationDao;
import com.dy.lock.screen.pojo.CustomerAuthenticationInfo;
import com.dy.lock.screen.service.CustomerAuthenticationService;
import com.dy.lock.screen.utils.CommonUtil;

@Service("customerAuthenticationService")
public class CustomerAuthenticationServiceImpl implements
		CustomerAuthenticationService {

	@Resource(name = "customerAuthenticationDao")
	private CustomerAuthenticationDao customerAuthenticationDao;

	@Override
	public CustomerAuthenticationInfo queryByPrimaryKey(String id) {
		return this.customerAuthenticationDao.queryByPrimaryKey(id);
	}

	@Override
	public CustomerAuthenticationInfo queryAuthStatus(
			CustomerAuthenticationInfo authenticationInfo) {
		// TODO Auto-generated method stub
		return this.customerAuthenticationDao
				.queryAuthStatus(authenticationInfo);
	}

	@Override
	public int save(CustomerAuthenticationInfo authInfo) {
		authInfo.setAid((CommonUtil.createRandomNum()));
		for (int i = 0; i < 20; i++) {
			CustomerAuthenticationInfo a = this.customerAuthenticationDao
					.queryByPrimaryKey(authInfo.getAid());
			if (a != null) {
				authInfo.setAid((CommonUtil.createRandomNum()));
			} else {
				break;
			}
		}
		authInfo.setApplyTime(new Date());
		authInfo.setApplyStatus(Constant.NOT_APPLYED);
		int re = this.customerAuthenticationDao.insert(authInfo);
		return re;
	}

	@Override
	public CustomerAuthenticationInfo queryAuthPassedStatus(
			CustomerAuthenticationInfo authenticationInfo) {
		// TODO Auto-generated method stub
		return this.customerAuthenticationDao
				.queryAuthPassedStatus(authenticationInfo);
	}

}
